package com.mlxg.config;/**
 * @author zcy
 * @date 2025/5/19 15:17
 */

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.zaxxer.hikari.HikariDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 *   @author zcy
 *   @date 2025/5/19 15:17
 *   @description: 多数据源配置
 */
//@Configuration
//@EnableTransactionManagement
//@Order(Ordered.HIGHEST_PRECEDENCE) // 最高优先级
public class DataSourceConfig {

    // 旧数据源配置（源库）
    @Bean(name = "oldDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
    public DataSource oldDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    // 新数据源配置（目标库）
    @Bean(name = "newDataSource")
    @DependsOn("masterDataSource") // 明确依赖顺序
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.slave")
    public DataSource newDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    // 动态数据源配置（关键修正部分）
    @Primary
    @Bean
    @DependsOn({"masterDataSource", "slaveDataSource"}) // 确保数据源先初始化
    public DataSource dynamicDataSource(
            @Qualifier("oldDataSource") DataSource oldDataSource,
            @Qualifier("newDataSource") DataSource newDataSource) throws Exception {

        DynamicRoutingDataSource dynamicDataSource = new DynamicRoutingDataSource();
        dynamicDataSource.addDataSource("master", oldDataSource);
        dynamicDataSource.addDataSource("slave", newDataSource);

        dynamicDataSource.setPrimary("master");
        dynamicDataSource.afterPropertiesSet();
        return dynamicDataSource;
    }

    // 事务管理器配置
    @Bean
    public PlatformTransactionManager transactionManager(
            @Qualifier("dynamicDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

